﻿using System;
using System.Linq;
using Thuoc24h.Data;

namespace Thuoc24h.Home.Pages
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginBtn.Click += new EventHandler(LoginBtn_Click);
        }

        void LoginBtn_Click(object sender, EventArgs e)
        {
            string username = UserNameTxt.Text.Trim();
            string pass = PasswordTxt.Text.Trim();
            MedLifeDataContext ct = new MedLifeDataContext();
            var customer = ct.Customers.FirstOrDefault(it => it.UserName == username && it.Password == pass);
            if (customer == null)
            {
                MessageLtr.Text = "Đăng nhập không thành công! Hãy kiểm tra lại.";
            }
            else
            {
                Session["UserName"] = username;
                Session["Customer"] = customer;
                if (Request.QueryString["ReturnUrl"]!=null)
                {
                    Response.Redirect(Request.QueryString["ReturnUrl"]);
                }
                else
                {
                    Response.Redirect("~/trang-chu.aspx");
                }
            }
        }
    }
}